package com.hxkj.cst.chengshuitong.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.hxkj.cst.chengshuitong.R;
import com.hxkj.cst.chengshuitong.ui.activity.RatePaymentActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：LuoYang
 * 时间：2017/5/31:14:59
 * 邮箱：18981443691@163.com
 * 说明：呵呵
 */
public class HomeRecyclerAdapter extends RecyclerView.Adapter<HomeRecyclerAdapter.ViewHolder> {
    private Context mContext;
    private static final long SCROLL_TIME = 3000;

    public HomeRecyclerAdapter(Context context) {
        mContext = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater
                .from(mContext)
                .inflate(R.layout.item_home_header, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        setUpConvenientBanner(holder.mConvenientBanner);
        setListener(holder);
    }

    private void setUpConvenientBanner(ConvenientBanner convenientBanner) {
        setConvenientBannerSize(convenientBanner);

        convenientBanner.setPages(new CBViewHolderCreator<ConvenientBannerAdapter>() {
            @Override
            public ConvenientBannerAdapter createHolder() {
                return new ConvenientBannerAdapter();
            }
        }, getCBDataSource())
                .setPageIndicator(new int[]{R.mipmap.ic_page_indicator, R.mipmap.ic_page_indicator_focused})
                .setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.CENTER_HORIZONTAL)
                .startTurning(SCROLL_TIME);
    }

    private void setConvenientBannerSize(ConvenientBanner convenientBanner) {
        WindowManager manager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(outMetrics);
        int windowsWidth = outMetrics.widthPixels;
        ViewGroup.LayoutParams layoutParams = convenientBanner.getLayoutParams();
        layoutParams.width = windowsWidth;
        layoutParams.height = windowsWidth * 130 / 320;
        convenientBanner.setLayoutParams(layoutParams);
    }

    private List<Integer> getCBDataSource() {
        List<Integer> integers = new ArrayList<>();
        TypedArray typedArray = mContext.getResources().obtainTypedArray(R.array.ConvenientBanners);
        for (int i = 0; i < typedArray.length(); i++) {
            integers.add(typedArray.getResourceId(i, R.mipmap.tfirst));
        }
        typedArray.recycle();
        return integers;
    }

    private void setListener(ViewHolder holder) {
        holder.mTvProximity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(mContext, "该功能暂未开放", Toast.LENGTH_SHORT).show();
            }
        });

        holder.mTvAddcar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(mContext, "该功能暂未开放", Toast.LENGTH_SHORT).show();
            }
        });

        holder.mTvArchives.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(mContext, "该功能暂未开放", Toast.LENGTH_SHORT).show();
            }
        });

        holder.mTvPaytax.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, RatePaymentActivity.class);
                mContext.startActivity(intent);
            }
        });

        holder.mTvPeccancy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(mContext, "该功能暂未开放", Toast.LENGTH_SHORT).show();
            }
        });
    }


    @Override
    public int getItemCount() {
        return 1;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private ConvenientBanner mConvenientBanner;
        private TextView mTvProximity;
        private TextView mTvPeccancy;
        private TextView mTvPaytax;
        private TextView mTvAddcar;
        private TextView mTvArchives;

        ViewHolder(View itemView) {
            super(itemView);
            initLayout(itemView);
        }

        private void initLayout(View itemView) {
            mConvenientBanner = (ConvenientBanner) itemView.findViewById(R.id.ihh_cb_contain);
            mTvProximity = (TextView) itemView.findViewById(R.id.ihh_tv_proximity);
            mTvPeccancy = (TextView) itemView.findViewById(R.id.ihh_tv_peccancy);
            mTvPaytax = (TextView) itemView.findViewById(R.id.ihh_tv_paytax);
            mTvAddcar = (TextView) itemView.findViewById(R.id.ihh_tv_addcar);
            mTvArchives = (TextView) itemView.findViewById(R.id.ihh_tv_archives);
        }
    }
}
